package com.pricy.me.model;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.Type;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


import android.content.Context;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;
import com.pricy.me.R;

public class ProductAction {
	private int id;
	private int action;
	private Date date;
	private int price;
	private String currency;
	private String by;
	private Product product;
	private User user;
	
	
	public int getId() {
		return id;
	}


	public void setId(int id) {
		this.id = id;
	}


	public int getAction() {
		return action;
	}


	public void setAction(int action) {
		this.action = action;
	}


	public Date getDate() {
		return date;
	}


	public void setDate(Date date) {
		this.date = date;
	}


	public int getPrice() {
		return price;
	}


	public void setPrice(int price) {
		this.price = price;
	}


	public String getCurrency() {
		return currency;
	}


	public void setCurrency(String currency) {
		this.currency = currency;
	}


	public String getBy() {
		return by;
	}


	public void setBy(String by) {
		this.by = by;
	}


	public Product getProduct() {
		return product;
	}


	public void setProduct(Product product) {
		this.product = product;
	}


	public User getUser() {
		return user;
	}


	public void setUser(User user) {
		this.user = user;
	}


	@SuppressWarnings("unchecked")
	public static List<ProductAction> load(int id, Context c) {
		List<ProductAction> actions = new ArrayList<ProductAction>();
		try {
			User user = new User();
			user.loadData(c);
			
			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ssZ").create();
			// Create a new HttpClient and Post Header
	        HttpClient httpclient = new DefaultHttpClient();
	         
	        final String lien = c.getResources().getString(R.string.lien);
			 HttpPost httppost = new HttpPost(lien+"api/product/price?id="+id);
			 HttpResponse response = httpclient.execute(httppost);
			
			String reponse = EntityUtils.toString(response.getEntity());
	    	 
	    	Type listType = new TypeToken<ArrayList<ProductAction>>(){}.getType();
			JsonParser parser = new JsonParser();
	        JsonArray array = parser.parse(reponse).getAsJsonArray();
	        actions = (ArrayList<ProductAction>) gson.fromJson(reponse, listType);
	        for (int i = 0; i < array.size(); i++) {
	        	User u = gson.fromJson(array.get(i).getAsJsonObject().get("user"), User.class);
	        	actions.get(i).setUser(u);
	        	Product p = gson.fromJson(array.get(i).getAsJsonObject().get("product"), Product.class);
	        	actions.get(i).setProduct(p);
			}
	        return actions;
		} catch (UnsupportedEncodingException e) {
			Log.e("ERROR", e.toString());
		} catch (ClientProtocolException e) {
			Log.e("ERROR", e.toString());
		} catch (UnknownHostException e) {
			Log.e("ERROR", e.toString());
		} catch (IOException e) {
			Log.e("ERROR", e.toString());
		}
		return actions;
	}
	
	
}
